//
//  AppDelegate.swift
//  ZHWeibo
//
//  Created by user on 15/12/5.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var Welobserver: NSObjectProtocol?
    var Oathobserver: NSObjectProtocol?
    var HomeObersever: NSObjectProtocol?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //初始化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //设置根控制器
        window?.rootViewController = ZHUserAccountViewModel.shareAccountInstance.userIsLogin ? defaultRootViewController() : ZHTabBarController()

        window?.makeKeyAndVisible()
        
        //接受通知
        /**
          * 参数1:通知的名字
          * 参数2:监听谁发送的通知
          * 参数3:queue代表后面的代码在哪个队列里面执行
                 如果为nil的话,就代表通知在哪个线程发出的,操作就在哪个线程中执行 
          * 参数4:通知接收到时,执行的代码
          BUG:
            (1)循环引用
            (2)该通知有返回值--销毁的时候需要注意
        */
        Welobserver = NSNotificationCenter.defaultCenter().addObserverForName(FromNewToWelController, object: nil, queue: nil, usingBlock: {[weak self] (NSNotification) -> Void in
            //接收到通知后,做一系列操作,不用再去实现方法
            self!.window?.rootViewController = ZHWelcomeController()
        })
        Oathobserver = NSNotificationCenter.defaultCenter().addObserverForName(fromOauthToWelcome, object: nil, queue: nil, usingBlock: {[weak self](NSNotification) -> Void in
            printLog("从OA跳转到Wel")
            self!.window?.rootViewController = ZHWelcomeController()
        })
        HomeObersever = NSNotificationCenter.defaultCenter().addObserverForName(FromWelcomeToHome, object: nil, queue: nil, usingBlock: {[weak self] (NSNotification) -> Void in
            //跳转到主控制器
            self!.window?.rootViewController = ZHTabBarController()
        })
        return true
    }
    //如果有新版本,就跳转新特性页,没有的话,就跳转直接跳转欢迎页面
    private func defaultRootViewController() -> UIViewController {
        //有新版本
        if isNewVersion() {
            return ZHNewFeatureController()
        }else{
            return ZHWelcomeController()
        }
    }
    
    
    //判断是否有新版本
    private func isNewVersion() -> Bool {
        //获取本地版本号
        let localVersionKey = "versionKey"
        let localVersion = NSUserDefaults.standardUserDefaults().stringForKey(localVersionKey)
        //取出当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //保存当前版本号
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: localVersionKey)
        if let local = localVersion where currentVersion.compare(local) != NSComparisonResult.OrderedDescending{
            return false
        }
        return true
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(Welobserver!)
        NSNotificationCenter.defaultCenter().removeObserver(Oathobserver!)
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

